<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/8/24
 * Time: 10:41 上午
 */

namespace App\Models\CacheModel\live;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

/**
 * Class LiveUserGiftBagRecord
 * @package App\Models\CacheModel\live
 */
class LiveUserGiftBagRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_user_gift_bag_record';
    protected $cacheModel = 'live.user_gift_bag_record';

    /**
     * 添加数据
     * @param $uid
     * @param $data
     * @return int|string
     * @throws \Exception
     */
    public function saveData($uid, $data)
    {
        try
        {
            $id = $this->insertGetId($data);
        }
        catch (\Exception $e)
        {
            $id = 0;
        }
        $this->listDataAdd($uid, [$id => $id]);
        return $id;
    }

    /**
     * 获取用户背包记录
     * @param int $uid
     * @return array|mixed
     * @throws \Exception
     */
    public function getUserGiftBagRecord(int $uid)
    {
        $page = input('page/d', 1);
        $dataList = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setPage($page)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($uid)
            {
                return $this->getInfoByIdsInData($ids, [$uid], $sortType);
            }, 'id', [], 0);
        $dataList['data'] = $this->resetUserGiftBagRecord($dataList['data']);
        return $dataList;
    }

    /**
     * 重组背包数据
     * @param $data
     * @return array
     * @throws \Exception
     */
    public function resetUserGiftBagRecord($data)
    {
        $gids = array_column($data, 'gid');
        $goods = (new LiveGift())->batchGetInfoByIds($gids);
        $res = [];
        foreach ($data as $key => $datum)
        {
            //1=>赠送使用 2=>平台任务获取 3=>直播福利获取
            $i = 1;
            switch ($datum['flag'])
            {
                case 1:
                    $desc = '赠送使用';
                    $i = -1;
                    break;
                case 2:
                    $desc = '平台任务获取';
                    break;
                case 3:
                    $desc = '直播福利获取';
                    break;
                case 4:
                    $desc = '系统赠送';
                    break;
                default:
                    $desc = '';
                    break;
            }
            $res[] = [
                'id' => $datum['id'],
                'name' => $goods[$datum['gid']]['name'] ?? '',
                'time' => date('Y.m.d-H:i', $datum['create_time']),
                'desc' => $desc,
                'num' => $i * $datum['change_num']
            ];
        }
        return $res;
    }

    /**
     * 批量获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $where = ['id' => ['in', $ids]];
            } else
            {
                $where = ['uid' => $type[0]];
            }
            $data = $this
                ->where($where)
                ->order('id', 'desc')
                ->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
